package org.jeecg.modules.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.bytebuddy.asm.Advice;
import org.jeecg.modules.content.entity.ConContentPerformance;
import org.jeecg.modules.content.entity.ConContentTemplate;
import org.jeecg.modules.content.entity.ConContentVersion;
import org.jeecg.modules.content.mapper.ConContentPerformanceMapper;
import org.jeecg.modules.content.mapper.ConContentTemplateMapper;
import org.jeecg.modules.content.mapper.ConContentVersionMapper;
import org.jeecg.modules.content.model.ConContentTemplateDetailModel;
import org.jeecg.modules.content.model.ConContentTemplateModel;
import org.jeecg.modules.content.service.IConContentTemplateService;
import org.jeecg.modules.content.service.IConContentVersionService;
import org.jeecg.modules.paper.entity.PaperLanding;
import org.jeecg.modules.paper.service.IPaperLandingService;
import org.jeecg.modules.task.mapper.TaskInfoBaseMapper;
import org.jeecg.modules.third.vo.ThirdContentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Description: 内容模版信息
 * @Author: wufanxin
 * @Date:   2021-07-06
 * @Version: V1.0
 */
@Service
public class ConContentTemplateServiceImpl extends ServiceImpl<ConContentTemplateMapper, ConContentTemplate> implements IConContentTemplateService {

    @Autowired
    private  ConContentTemplateMapper templateMapper;
    @Autowired
    private ConContentPerformanceMapper conContentPerformanceMapper;
    @Autowired
    private IPaperLandingService paperLandingService;
    @Autowired
    private TaskInfoBaseMapper taskInfoBaseMapper;

    @Override
    public Page<ConContentTemplateModel> getPerList(Page<ConContentTemplateModel> page, ConContentTemplateModel conContentTemplateModel) {
        return page.setRecords(templateMapper.getPerList(page,conContentTemplateModel));
    }

    @Override
    public Page<ThirdContentVo> getPerList(Page<ThirdContentVo> page, Integer type,String userId) {
        return page.setRecords(templateMapper.getThirdList(page,type,userId));
    }

    @Override
    public ConContentTemplateDetailModel getDetail(String templateId) {
        ConContentTemplateDetailModel  conContentTemplateDetailModel = new ConContentTemplateDetailModel();
        ConContentTemplate conContentTemplate = templateMapper.selectById(templateId);



        // conContentTemplate.setAuthorName(sysUserMapper.getUserByName(conContentTemplate.getAuthorName()).getRealname());
        conContentTemplateDetailModel.setConContentTemplate(conContentTemplate);
        QueryWrapper<ConContentPerformance> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("template_id",templateId);
        ConContentPerformance conContentPerformance = conContentPerformanceMapper.selectOne(queryWrapper);

        if(conContentPerformance !=null){
            QueryWrapper<PaperLanding> paperLandingQueryWrapper = new QueryWrapper<>();
            paperLandingQueryWrapper.eq("content_id",conContentPerformance.getId());
            PaperLanding paperLanding = paperLandingService.getOne(paperLandingQueryWrapper);
            if(paperLanding !=null){
                conContentPerformance.setPaperTitle(paperLanding.getTitle());
                conContentPerformance.setPaperId(paperLanding.getId());
            }
        }


        List<String> list = taskInfoBaseMapper.selectUnionNames(conContentTemplate.getContentNo());
        String unionName = "";
        if(!CollectionUtils.isEmpty(list)){
            for (String s : list) {
                unionName += s;

            }
        }
        conContentTemplateDetailModel.setConContentPerformance(conContentPerformance);
        conContentTemplateDetailModel.setUnionPeople(unionName);

        return  conContentTemplateDetailModel;
    }
}
